<?php
class Editor {
	/**
	* @var mixed Mysql connection
	*/
	var $mysqli = null;
	/**
	* @var array User credentials (id, email, type)
	*/
	var $user = null;
	/**
	* @var array Notifications
	*/
	var $notif = array();
	/**
	* @var string Area of concentration
	*/
	var $area = null;
	/**
	* @var string Database table to be used
	*/
	var $table = null;
	
	/**
	* Editor constructor
	* Initializes editor variables for intended request
	* 
	* @param string $area Area of concentration
	* @param resource $mysqli Current mysql connection
	* @param resource $user User credentials (id, email, type)
	*/
	function __construct($area, $mysqli, $user) {
		require_once "class/setting.php";
		require_once "class/section.php";
		require_once "class/project.php";
		require_once "class/tool.php";
		
		$this->mysqli = $mysqli;
		$this->user = $user;
		$this->area = $area;
		
		switch($area) {
			# Microorganism
			case 'micro':
				$this->table = 'MicroCoeds';
			break;
			
			# Project
			case 'proj':
				$this->table = 'ProjCoeds';
			break;
			
			# Authors
			case 'author':
				$this->table = 'ProjAuthors';
			break;
			
			# Images
			case 'img':
				$this->table = 'ImgCoeds';
			break;
			
			# Documents
			case 'doc':
				$this->table = 'DocCoeds';
			break;
			
			# Locations
			case 'loc':
				$this->table = 'LocCoeds';
			break;
			
			# Resources
			case 'rsrc':
				$this->table = 'RsrcCoeds';
			break;
			
			# Editors
			default:
				$this->table = 'Editors';
			break;
		}
	}
	
	/**
	* Search editor
	* Search an editor from the system
	* 
	* @access public
	* @param string $query Query string
	* @param array $opts Parameters
	* @param string $start Starting row
	* @param string $limit Limitation
	* 
	* @return array Search results, number of results
	*/
	public function search($query, $opts, $start, $limit) {
		# Replaces all comma's to make valid query string
		$query = is_array($query) ? $query : str_replace(',', ' ', $query);
		# If edtr is specified, set table to Editors; else, default to Editors
		$table = empty($opts['acct']) ? "Editors" : "Accounts";
		# Selects size of returned rows in mysql search
		$selSize = "SELECT FOUND_ROWS()";
		
		# Set order according to name or something else
		$opts['ob'] = isset($opts['ob']) ? $opts['ob'] : 'Lname, Fname';
		# According to ascending or descending order
		$opts['oa'] = isset($opts['oa']) ? $opts['oa'] : 'ASC';
		
		# Search for an Account ID
		if(!empty($opts['c'])) {
			if($opts["c"]=="id") {
				$sel = "SELECT SQL_CALC_FOUND_ROWS * FROM Editors WHERE ID='{$query}' ".(empty($opts['id']) ? '' : "OR ID='{$opts['id']}'");
			}
			elseif($opts['c']=="multi-id") {
				$sel = "SELECT SQL_CALC_FOUND_ROWS * FROM Editors WHERE ID IN ('".implode("','", $query)."') ORDER BY {$opts['ob']} {$opts['oa']}";
			}
			elseif($opts['c']=="remove") {
				$sel = "SELECT SQL_CALC_FOUND_ROWS * FROM Editors WHERE Remove='1' ORDER BY {$opts['ob']} {$opts['oa']}";
			}
		}
		else {
			# Part query from string into an array based on spaces contained
			$arrQuery = is_array($query) ? explode(" ", $query) : array($query);
			
			# Set conditional query if any condition is needed
			$cond = " WHERE ";
			# Set query string on Email, Lname, and Fname
			foreach($arrQuery as $query) {
				$query = Tool::trimout($query);
				$cond .= " ID='{$query}' OR Email LIKE '%{$query}%' OR ";
				$cond .= " Lname LIKE '%{$query}%' OR ";
				$cond .= " Fname LIKE '%{$query}%' OR ";
				$cond .= " Mname LIKE '%{$query}%' AND ";
			}
			
			# Remove final OR in query string
			# Start and End of row search
			$start = isset($start) ? $start : '0';
			$limit = isset($limit) ? $limit : '10';
			
			# Removed items
			$cond .= (in_array($this->user['type'], array('ADMIN','PRIME')) and $opts['c']=='remove') ? " Remove='1' " : " Remove='0' ";
			
			# Query string, combined with conditions and order parameters
			$sel = "SELECT SQL_CALC_FOUND_ROWS * FROM Editors {$cond} ORDER BY {$opts['ob']} {$opts['oa']} LIMIT {$start}, {$limit}";
		}
		
		# Perform a mysql query on passed query string
		$res = $this->mysqli->query($sel);
		# Get size of rows in mysql returned resource
		$size = $this->mysqli->query($selSize);
		$size = $size->fetch_row();
		
		# Return all returned and size of rows
		return array($res, $size[0]);
	}
	
	/**
	* Create editor
	* Create a new editor
	* 
	* @access public
	* @param string $type Type
	* @param string $lname Last name
	* @param string $fname First name
	* @param string $mname Mid name
	* 
	* @return mixed ID on success, false on failure
	*/
	public function create($lname, $fname, $mname, $auth, $edtr) {
		$id = Tool::generateID('edtr');
		$auth = $auth ? '1' : '0';
		$edtr = $edtr ? '1' : '0';
		
		# Create new editor
		$ins = "
			INSERT INTO Editors VALUES (
				'{$id}', NULL, '{$auth}', '{$edtr}', '".Tool::trimout($lname)."', '".Tool::trimout($fname)."', '".Tool::trimout($mname)."',
				NULL, NULL, '0', '{$this->user['id']}', '".date('Y-m-d H:i:s:ms')."', '0'
			)
		";
		
		# Do mysql query
		if(!$this->mysqli->query($ins)) {
			$this->notif = 'ERROR: ins edtr';
		}
		
		# Created successfully
		if(empty($this->notif)) {
			return $id;
		}
		
		# Error occurred
		//echo '<pre>', var_dump($this->notif), '</pre>';
		return false;
	}
	
	/**
	* Update editor
	* Update an existing editor
	* 
	* @access public
	* @param string $id ID of editor
	* @param string $lname Last name
	* @param string $fname First name
	* @param string $mname Middle name
	* @param string $email Email address
	* @param string $auth Author flag
	* @param string $edtr Editor flag
	* 
	* @return mixed id on success, false on failure
	*/
	public function update($id, $lname, $fname, $mname, $email, $auth, $edtr) {
		# Update editor string
		$upd = "
			UPDATE Editors SET
				Lname='".mysql_real_escape_string($lname)."',
				Fname='".mysql_real_escape_string($fname)."',
				Mname='".mysql_real_escape_string($mname)."',
				Email='".mysql_real_escape_string($email)."',
				Author='{$auth}', Editor='{$edtr}',
				UpdBy='{$this->user['id']}', UpdOn='".date('Y-m-d H:i:s:ms')."', UpdNo=UpdNo+1
			WHERE ID='{$id}'
		";
		
		# Update editor in mysql database
		if(!$this->mysqli->query($upd)) {
			$this->notif[] = "Error in updating details of editor.<br/>";
		}
		
		# Notifications found; Error occurred
		if(!empty($this->notif)) {
			//echo '<pre>', var_dump($this->notif), '</pre>';
			return false;
		}
		
		# Updated editor successfully
		return $id;
	} # function update
	
	/**
	* Process appended and remove editors
	* Process on all editors associated with an area
	* which are to be appended or removed
	* 
	* @access public
	* @param array $addl Editors to be appended
	* @param array $rmvl Editors to be removed
	* @param string $id ID of area
	*/
	public function operate($addl, $rmvl, $id, $new) {
		if(!empty($addl["val"])) $this->append($addl, $id, $new);
		if(!empty($rmvl)) $this->remove($rmvl, $id);
	}
	
	/**
	* Append editors
	* Append editors associated to that area
	* 
	* @access public
	* @param array $addl Editors to be appended
	* @param string $id ID of area
	* @param boolean $new Create editor if non-existent
	*/
	public function append($addl, $id, $new) {
		# Iterate through each editor
		foreach($addl['val'] as $i => $value) {
			# Empty name, skip
			if(empty($value)) continue;
			
			# Append throught editor's ID
			if(!empty($addl['id'][$i])) {
				$ins = "INSERT INTO {$this->table} VALUES ('{$id}', '{$addl['id'][$i]}', '1', '{$this->user['id']}', '".date('Y-m-d H:i:s:ms')."')";
			}
			# Editor's name
			elseif(strrpos($value, ',') > 1) {
				$name = explode(",", $value);
				
				if(count($name) === 3) {
					# Search from editors/accounts
					$sel = "SELECT ID FROM ".(!empty($new) ? 'Editors' : 'Accounts')." WHERE Lname='".Tool::trimout($name[0])."' AND Fname='".Tool::trimout($name[1])."'";
					$res = $this->mysqli->query($sel);
					
					# Found name
					if($res and !empty($res->num_rows)) {
						while($obj = $res->fetch_object()) {
							$ins = "INSERT INTO {$this->table} VALUES ('{$id}', '{$obj->ID}', '1', '{$this->user['id']}', '".date('Y-m-d H:i:s:ms')."')";
						}
					}
					# Not found, create
					elseif(isset($new) and $new) {
						if(($newID = $this->create('AUTHOR', $name[0], $name[1], $name[2]))!==false) {
							$ins = "INSERT INTO {$this->table} VALUES ('{$id}', '{$newID}', '0', '{$this->user['id']}', '".date('Y-m-d H:i:s:ms')."')";
						}
						else {
							$this->notif[] = "Error adding editors.<br/>";
						}
					}
				}
			}
			# Editor's Email
			elseif(strrpos($value, '@') > 1) {
				$sel = "SELECT ID FROM Accounts WHERE Email='{$value}'";
				$res = $this->mysqli->query($sel);
				
				# Account found
				if($res and !empty($res->num_rows)) {
					while($obj = $res->fetch_object()) {
						$ins = "INSERT INTO {$this->table} VALUES ('{$id}', '{$obj->ID}', '1', '{$this->user['id']}', '".date('Y-m-d H:i:s:ms')."')";
					}
				}
			}
			
			# Do mysql query
			if(!empty($ins)) {
				if(!$this->mysqli->query($ins)) {
					$this->notif[] = "ERROR: ins area-img $id $value.<br/>";
				}
			}
		}
	}
	
	/**
	* Remove editors
	* Remove editors associated to that area
	* 
	* @access public
	* @param array $rmvl Editors to be removed
	* @param string $id ID of area
	*/
	public function remove($rmvl, $id) {
		# Remove editors assoc. with an area
		$del = "DELETE FROM {$this->table} WHERE PID='{$id}' AND CID IN ('".implode("', '", $rmvl)."')";
		
		if(!$this->mysqli->query($del)) {
			$this->notif[] = "ERROR: rem micro<br/>";
		}
	}
	
	/**
	* Display all editors
	* Retrieve all contents of editors on a page
	* 
	* @access public
	* @param array $queries IDs of editors
	*/
	public function displayAll($queries, $opts) { ?>
		<output> <?php
			if($results = $this->search($queries, $opts, 0, 10)) {
				while($result = $results[0]->fetch_object()) {
					$this->displayContent($result, false);
				}
			} ?>
		</output> <?php
	}
	
	/**
	* Guess editors
	* Provides a list of editors (guess) on user input
	* Makes selection of resource and id faster
	* 
	* @access public
	*/
	public function guess($res) {
		# List all of editor guesses
		while($obj = $res->fetch_object()) { ?>
			<div class="guess edtr" data-id="<?php echo $obj->ID ?>" data-value="<?php echo $obj->Lname.', '.$obj->Fname.', '.substr($obj->Mname, 0, 1) ?>" tabindex="0"> <?php
				echo $obj->Lname.', '.$obj->Fname.', '.substr($obj->Mname, 0, 1) ?>
			</div> <?php
		}
	}
	
	/**
	* Retrieve details
	* Get all details associated with projects to be displayed
	* 
	* @access private
	* @param string $area Area where project is associated
	* @param string $id ID of project
	* @param mixed $proj Project object
	* @param string $cond Additional constraints
	* @param string $limit Limit to search results
	* 
	* @return mixed Returned results on search
	*/
	private function retrieve($type, $area, $id, $cond, $limit) {
		if($type=='edtr') {
			# Retrieve contents
			switch($area) {
				# Microorganism
				case 'micro':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Microorganisms AS T1 INNER JOIN Accounts AS T2
							ON T1.Crtby=T2.ID WHERE T2.EID='{$id}' AND T1.Remove='0' ORDER BY T1.CrtOn {$limit}
					";
				break;
				
				# Images
				case 'img':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Images AS T1 INNER JOIN Accounts AS T2
							ON T1.Crtby=T2.ID WHERE T2.EID='{$id}' AND T1.Remove='0' ORDER BY T1.CrtOn {$limit}
					";
				break;
				
				# Documents
				case 'doc':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Documents AS T1 INNER JOIN Accounts AS T2
							ON T1.Crtby=T2.ID WHERE T2.EID='{$id}' AND T1.Remove='0' ORDER BY T1.CrtOn {$limit}
					";
				break;
				
				# Locations
				case 'loc':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Locations AS T1 INNER JOIN Accounts AS T2
							ON T1.Crtby=T2.ID WHERE T2.EID='{$id}' AND T1.Remove='0' ORDER BY T1.CrtOn {$limit}
					";
				break;
				
				# Resources
				case 'rsrc':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Resources AS T1 INNER JOIN Accounts AS T2
							ON T1.Crtby=T2.ID WHERE T2.EID='{$id}' AND T1.Remove='0' ORDER BY T1.CrtOn {$limit}
					";
				break;
				
				# Project
				case 'proj':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Projects AS T1 INNER JOIN Accounts AS T2
							ON T1.Crtby=T2.ID WHERE T2.EID='{$id}' AND T1.Remove='0' ORDER BY T1.CrtOn {$limit}
					";
				break;
			}
		}
		elseif($type=='coed') {
			# Retrieve project-related contents
			switch($area) {
				# Microorganism
				case 'micro':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Microorganisms AS T1 INNER JOIN MicroCoeds AS T2 INNER JOIN Accounts AS T3
							ON T1.ID=T2.PID AND T2.CID=T3.ID WHERE T3.EID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
					";
				break;
				
				# Images
				case 'img':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Images AS T1 INNER JOIN ImgCoeds AS T2 INNER JOIN Accounts AS T3
							ON T1.ID=T2.PID AND T2.CID=T3.ID WHERE T3.EID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
					";
				break;
				
				# Documents
				case 'doc':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Documents AS T1 INNER JOIN DocCoeds AS T2 INNER JOIN Accounts AS T3
							ON T1.ID=T2.PID AND T2.CID=T3.ID WHERE T3.EID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
					";
				break;
				
				# Locations
				case 'loc':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Locations AS T1 INNER JOIN LocCoeds AS T2 INNER JOIN Accounts AS T3
							ON T1.ID=T2.PID AND T2.CID=T3.ID WHERE T3.EID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
					";
				break;
				
				# Resources
				case 'rsrc':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Resources AS T1 INNER JOIN RsrcCoeds AS T2 INNER JOIN Accounts AS T3
							ON T1.ID=T2.PID AND T2.CID=T3.ID WHERE T3.EID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
					";
				break;
				
				# Projects
				case 'proj':
					$sel = "
						SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Projects AS T1 INNER JOIN ProjCoeds AS T2 INNER JOIN Accounts AS T3
							ON T1.ID=T2.PID AND T2.CID=T3.ID WHERE T3.EID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
					";
				break;
			}
		}
		elseif($type=='auth') {
			$sel = "
				SELECT SQL_CALC_FOUND_ROWS DISTINCT T1.* FROM Projects AS T1 INNER JOIN ProjAuthors AS T2
					ON T1.ID=T2.PID AND T2.CID='{$id}' AND T1.Remove='0' {$cond} ORDER BY T2.CrtOn {$limit}
			";
		}
		
		# Retrieve in mysql
		$res = $this->mysqli->query($sel);
		$size = $this->mysqli->query("SELECT FOUND_ROWS()");
		$size = $size->fetch_row();
		
		# Return results and size
		return array($res, $size[0]);
	} # function retrieve
	
	/**
	* Display main microorganism stage
	* List all editors in the main page
	* 
	* @access public
	* @param mixed $edtrs List of editors from returned search results
	* @param integer $size Size of rows from returned search results
	* @param string $query Query string that was searched for
	* @param array $opts Optional parameters
	* @param integer $start Starting row in mysql returned results
	*/
	public function displayMain($edtrs, $size, $query, $opts, $start) {
		# Check if one microorganism is to be displayed
		$view = $edtrs->num_rows==1;
		# Get page number
		$page = ceil(($start+1)/10);
		# Prepare URL variables
		$optstr = Tool::optstrify($opts); ?>
		
		<div id="side"> <?php
			# Show Back and Next buttons for navigation ?>
			<div id="navis"> <?php
				# Links to previous ten items ?>
				<a href="main.php?<?php echo $optstr.(empty($query) ? '' : '&q='.$query).(empty($start) ? '&s=0' : '&s='.(intval($start)-10)) ?>"
					class="navi ui ui-button prev <?php if(empty($start)) echo 'disabled' ?>" tabindex="-1" title="Previous 10 items"></a> <?php
				# Links to next ten items ?>
				<a href="main.php?<?php echo $optstr.(empty($start) ? '&s=10' : '&s='.(intval($start)+10)) ?>"
					class="navi ui ui-button next <?php if(($edtrs->num_rows<10) or ($edtrs->num_rows*$page)>=$size) { echo 'disabled'; } ?>" tabindex="-1" title="Next 10 items"></a>
			</div> <?php
			
			# Knobs to stage an editor ?>
			<div id="knobs"> <?php $i = 1;
				while($edtr = $edtrs->fetch_object()) {
					# Contains data query name, id, tool (tooltip help)
					$name = Tool::truncate($trname = ($edtr->Lname.', '.$edtr->Fname.' '.substr($edtr->Mname, 0, 1).'.'), 80); ?>
					<div id="knob<?php echo $edtr->ID ?>" class="knob micro <?php if($view) echo "lock" ?>"
						data-id="<?php echo $edtr->ID ?>" data-tool="{'head':'<b class=\'cool\'><?php echo $name ?></b>','x':'l'}">
						<?php echo $i++; ?>
					</div> <?php
				} ?>
			</div>
		</div> <?php
		
		# Labels stage items (header) ?>
		<div id="hnotif" class="escape"> <?php
			# Shows metadata of all searched items ?>
			<span id="query" class="escape iblock" data-tool="{'head':'<?php echo ($all = (empty($query) and empty($opts['q']))) ? "All editors" : (empty($opts['q']) ? $trname : $opts['q']) ?>','x':'l'}"
				<?php if(!($nctrl = empty($_COOKIE['ctrl']['search']))) { echo 'style="width:400px"'; } ?>>
				<?php echo (isset($opts['rnk']) ? $this->taxalabel[$opts['rnk']].': ' : '')
					.($all ? "All authors and editors" : ((isset($opts['view']) or $view) ? $name : (empty($_REQUEST['htitle']) ? $query : $_REQUEST['htitle']))) ?>
			</span>
			
			<span>
				<span id="search-ctrls" <?php if($nctrl) { echo 'class="none"'; } ?>> <?php
					# No. of returned results
					echo '<span id="numresults">Showing '.($edtrs->num_rows).' of <b>'.$size.'</b> result(s)</span> on page ';
					
					# Many editors are to be viewed
					$ob = isset($_REQUEST['ob']); $of = true;
					unset($opts['ob']); unset($opts['oa']);
					$optstr = Tool::optstrify($opts);
					
					# Prepare filter options and page selector ?>
					<select id="main-page" class="page navi" data-tool="{'head':'Page'}"> <?php
						for($i=0, $x=$size/10; $i<$x; $i++)	echo "<option ".((isset($opts['s']) and $opts['s']==($i*10)) ? 'selected="selected"' : '').'value="main.php?'.$optstr.(empty($query) ? '' : '&q='.$query).'&s='.($i*10).'">'.($i+1).'</option>';?>
					</select> sorted by <?php
					
					# Prepare filter options ?>
					<select class="navi" data-tool="{'head':'Sort'}">
						<option value="main.php?<?php echo $optstr ?>&ob=name&oa=asc" <?php if($ob and $_REQUEST['ob']=='name') { echo 'selected="selected"'; $of = false; } ?>>Name</option>
						<option value="main.php?<?php echo $optstr ?>&ob=crtby&oa=asc" <?php if($of and $ob and $_REQUEST['ob']=='crtby') { echo 'selected="selected"'; $of = false; } ?>>Author</option>
						<option value="main.php?<?php echo $optstr ?>&ob=crton&oa=desc" <?php if($of and $ob and $_REQUEST['ob']=='crton') { echo 'selected="selected"'; $of = false; } ?>>Date created</option>
						<option value="main.php?<?php echo $optstr ?>&ob=id&oa=asc" <?php if($of and $ob and $_REQUEST['ob']=='id') { echo 'selected="selected"'; $of = false; } ?>>ID</option>
					</select>
				</span>
				<span <?php if(!$nctrl) { echo 'class="none"'; } ?>><?php echo "$edtrs->num_rows of <b>$size</b> result(s) on page <b>$page</b>";  ?></span> <button type="button" class="navi-details <?php echo (!$nctrl ? 'selected' : '') ?>" data-tool="{'head':'Search controls'}"></button>
			</span>
		</div> <?php
		
		# Contains all editor headers ?>
		<div id="stage"> <?php
			# Reset pointer to first row in mysql result
			$edtrs->data_seek(0);
			
			# List all editors on its own entry page
			while($edtr = $edtrs->fetch_object()) {
				# Show first the heading of each editor
				$this->displayHeading($edtr, $view);
			} ?>
		</div> <?php
	} # function displayMain
	
	/**
	* Display a editor heading
	* Displays a heading for each editor in the results
	* which contains the editor's name
	* 
	* @access public
	* @param mixed $edtr Contains all details of the editor
	* @param boolean $cont Checks if content is to be displayed too
	*/
	public function displayHeading($edtr, $cont) {
		# List a microorganism first as an area with a header that can be selected/clicked by the user in the stage
		# Data included: head (scientific name), id (microorganism id)
		$name = $edtr->Lname.', '.$edtr->Fname.' '.substr($edtr->Mname, 0, 1).'.';
		?> <div id="area<?php echo $edtr->ID ?>" class="stage area edtr" data-head="<?php echo $name ?>">
			<div class="div-header edtr <?php echo $cont ? "active lock" : "heading" ?>" data-id="<?php echo $edtr->ID ?>" data-content="edtr"
				<?php if(strlen($name)>100) echo 'data-tool="{\'head\':\''.$name.'\',\'x\':\'l\'}"' ?>>
				<a href="main.php?edtr=view&id=<?php echo $edtr->ID ?>" class="name" tabindex="-1">
					<?php echo $name ?></a> <span class="email right"><?php echo empty($edtr->Email) ? '' : $edtr->Email ?></span>
			</div> <?php
			
			# Show microorganism's contents if necessary
			if($cont) {
				$this->displayContent($edtr, $cont);
			} ?>
		</div> <?php
	} # function displayHeading
	
	/**
	* Display contents of editor
	* Displays the contents of a editor, from its scientific name
	* to its associated items in other areas of the system
	* 
	* @access public
	* @param mixed $edtr Contains all details of the editor
	* @param boolean $cont Checks if only one editor is found in results
	*/
	public function displayContent($edtr, $cont) { ?>
		<div class="div-content">
			<form id="form-edtr<?php echo $edtr->ID ?>" class="edtr" name="form-edtr" method="post" action="process.php?edtr=edit" enctype="multipart/form-data" novalidate="novalidate">
			<div class="div-upper">
				<div class="div-body"> <?php
					if(!empty($edtr->Author)) {
						list($resArticle, $sizeArticle) = $this->retrieve('auth', 'proj', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = empty($sizeArticle)) ? "on" : "off open" ?> next">
							<b>Research articles</b> <span><?php echo $sizeArticle ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>">
							<label>Authored:</label> <?php
							if(!$empty) { ?>
								<ol class="container"> <?php
									if(empty($sizeArticle)) {
										echo 'None';
									}
									else {
										while($obj = $resArticle->fetch_object()) { ?>
											<li class="proj">
												<a href="main.php?proj=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Title ?></a><br/>
												<span class="dim">Abstract: <?php echo Tool::truncate($obj->Abstr, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
					}
					
					if(!empty($edtr->Editor)) {
						list($resMicroAuth, $sizeMicroAuth) = $this->retrieve('edtr', 'micro', $edtr->ID, '', ''); 
						list($resMicroCoed, $sizeMicroCoed) = $this->retrieve('coed', 'micro', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = (empty($sizeMicroAuth) and empty($sizeMicroCoed))) ? "on" : "off open" ?> next">
							<b>Microorganisms</b> <span><?php echo ($sizeMicro = ($sizeMicroAuth + $sizeMicroCoed)) ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>"> <?php
							if(!$empty) { ?>
								<label>Owned:</label>
								<ol class="container"> <?php
									if(empty($sizeMicroAuth)) {
										echo 'None';
									}
									else {
										while($obj = $resMicroAuth->fetch_object()) { ?>
											<li class="micro">
												<a href="main.php?micro=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Name ?></a>
												<span class="dim"><?php echo $obj->Authors ?></span>
											</li> <?php
										}
									} ?>
								</ol>
								
								<label>Co-edited:</label>
								<ol class="container"> <?php
									if(empty($sizeMicroCoed)) {
										echo 'None';
									}
									else {
										while($obj = $resMicroCoed->fetch_object()) { ?>
											<li class="micro">
												<a href="main.php?micro=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Name ?></a>
												<span class="dim"><?php echo $obj->Authors ?></span>
											</li> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
						
						list($resImgAuth, $sizeImgAuth) = $this->retrieve('edtr', 'img', $edtr->ID, '', ''); 
						list($resImgCoed, $sizeImgCoed) = $this->retrieve('coed', 'img', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = (empty($sizeImgAuth) and empty($sizeImgCoed))) ? "on" : "off open" ?> next">
							<b>Images</b> <span><?php echo ($sizeImg = ($sizeImgAuth + $sizeImgCoed)) ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>"> <?php
							if(!$empty) { ?>
								<label>Owned:</label>
								<ol class="container"> <?php
									if(empty($sizeImgAuth)) {
										echo 'None';
									}
									else {
										while($obj = $resImgAuth->fetch_object()) { ?>
											<a href="main.php?img=view&id=<?php echo $obj->ID ?>">
											<img class="stage file locked ghost" src="<?php echo empty($obj->Thumb) ? 'template/img.jpg' : $obj->Thumb ?>" alt="<?php echo $obj->Title ?>" style="width:<?php echo (125/$obj->Height)*$obj->Width ?>px"
												data-id="<?php echo $obj->ID ?>" data-src="<?php echo $obj->Path ?>" data-visib="<?php echo $obj->Visib ?>" data-categ="<?php echo $obj->Categ ?>"
												data-tool="{'head':'<span class=\'permalink\'><a href=\'main.php?img=view&id=<?php echo $obj->ID ?>\' target=\'_blank\'><?php echo $obj->Title ?></a></span>','stay':'true','x':'l'}"/></a> <?php
										}
									} ?>
								</ol>
								
								<label>Co-edited:</label>
								<ol class="container"> <?php
									if(empty($sizeImgCoed)) {
										echo 'None';
									}
									else {
										while($obj = $resImgCoed->fetch_object()) { ?>
											<a href="main.php?img=view&id=<?php echo $obj->ID ?>">
											<img class="stage file locked ghost" src="<?php echo empty($obj->Thumb) ? 'template/img.jpg' : $obj->Thumb ?>" alt="<?php echo $obj->Title ?>" style="width:<?php echo (125/$obj->Height)*$obj->Width ?>px"
												data-id="<?php echo $obj->ID ?>" data-src="<?php echo $obj->Path ?>" data-visib="<?php echo $obj->Visib ?>" data-categ="<?php echo $obj->Categ ?>"
												data-tool="{'head':'<span class=\'permalink\'><a href=\'main.php?img=view&id=<?php echo $obj->ID ?>\' target=\'_blank\'><?php echo $obj->Title ?></a></span>','stay':'true','x':'l'}"/></a> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
						
						list($resDocAuth, $sizeDocAuth) = $this->retrieve('edtr', 'doc', $edtr->ID, '', ''); 
						list($resDocCoed, $sizeDocCoed) = $this->retrieve('coed', 'doc', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = (empty($sizeDocAuth) and empty($sizeDocCoed))) ? "on" : "off open" ?> next">
							<b>Documents</b> <span><?php echo ($sizeDoc = ($sizeDocAuth + $sizeDocCoed)) ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>"> <?php
							if(!$empty) { ?>
								<label>Owned:</label>
								<ol class="container"> <?php
									if(empty($sizeDocAuth)) {
										echo 'None';
									}
									else {
										while($obj = $resDocAuth->fetch_object()) { ?>
											<li class="doc">
												<a href="main.php?doc=view&id=<?php echo $obj->ID ?>" target="_blank" data-title="<?php echo $obj->Title ?>" title="<?php echo $obj->Title ?>"
													data-doc="<?php echo $obj->Path ?>"><?php echo empty($obj->Ref) ? $obj->Title : $obj->Ref ?></a><br/>
												<span class="dim">Abstract: <?php echo Tool::truncate($obj->Abstr, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol>
								
								<label>Co-edited:</label>
								<ol class="container"> <?php
									if(empty($sizeDocCoed)) {
										echo 'None';
									}
									else {
										while($obj = $resDocCoed->fetch_object()) { ?>
											<li class="doc">
												<a href="main.php?doc=view&id=<?php echo $obj->ID ?>" target="_blank" data-title="<?php echo $obj->Title ?>" title="<?php echo $obj->Title ?>"
													data-doc="<?php echo $obj->Path ?>"><?php echo empty($obj->Ref) ? $obj->Title : $obj->Ref ?></a><br/>
												<span class="dim">Abstract: <?php echo Tool::truncate($obj->Abstr, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
						
						list($resLocAuth, $sizeLocAuth) = $this->retrieve('edtr', 'loc', $edtr->ID, '', ''); 
						list($resLocCoed, $sizeLocCoed) = $this->retrieve('coed', 'loc', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = (empty($sizeLocAuth) and empty($sizeLocCoed))) ? "on" : "off open" ?> next">
							<b>Locations</b> <span><?php echo ($sizeLoc = ($sizeLocAuth + $sizeLocCoed)) ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>"> <?php
							if(!$empty) { ?>
								<label>Owned:</label>
								<ol class="container"> <?php
									if(empty($sizeLocAuth)) {
										echo 'None';
									}
									else {
										while($obj = $resLocAuth->fetch_object()) { ?>
											<li class="loc">
												<a href="main.php?loc=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Name ?></a>
												<span class="dim"><?php echo '('.$obj->Lat.', '.$obj->Lng.')' ?></span>
											</li> <?php
										}
									} ?>
								</ol>
								
								<label>Co-edited:</label>
								<ol class="container"> <?php
									if(empty($sizeLocCoed)) {
										echo 'None';
									}
									else {
										while($obj = $resLocCoed->fetch_object()) { ?>
											<li class="loc">
												<a href="main.php?loc=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Name ?></a>
												<span class="dim"><?php echo '('.$obj->Lat.', '.$obj->Lng.')' ?></span>
											</li> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
						
						list($resRsrcAuth, $sizeRsrcAuth) = $this->retrieve('edtr', 'rsrc', $edtr->ID, '', ''); 
						list($resRsrcCoed, $sizeRsrcCoed) = $this->retrieve('coed', 'rsrc', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = (empty($sizeRsrcAuth) and empty($sizeRsrcCoed))) ? "on" : "off open" ?> next">
							<b>Resources</b> <span><?php echo ($sizeRsrc = ($sizeRsrcAuth + $sizeRsrcCoed)) ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>"> <?php
							if(!$empty) { ?>
								<label>Owned:</label>
								<ol class="container"> <?php
									if(empty($sizeRsrcAuth)) {
										echo 'None';
									}
									else {
										while($obj = $resRsrcAuth->fetch_object()) { ?>
											<li class="rsrc">
												<a href="main.php?rsrc=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Title ?></a><br/>
												<span class="dim">Notes: <?php echo Tool::truncate($obj->Notes, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol>
								
								<label>Co-edited:</label>
								<ol class="container"> <?php
									if(empty($sizeRsrcCoed)) {
										echo 'None';
									}
									else {
										while($obj = $resRsrcCoed->fetch_object()) { ?>
											<li class="rsrc">
												<a href="main.php?rsrc=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Title ?></a><br/>
												<span class="dim">Notes: <?php echo Tool::truncate($obj->Notes, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
						
						list($resProjAuth, $sizeProjAuth) = $this->retrieve('edtr', 'proj', $edtr->ID, '', ''); 
						list($resProjCoed, $sizeProjCoed) = $this->retrieve('coed', 'proj', $edtr->ID, '', ''); ?>
						
						<div class="div-peel peel <?php echo ($empty = (empty($sizeProjAuth) and empty($sizeProjCoed))) ? "on" : "off open" ?> next">
							<b>Projects</b> <span><?php echo ($sizeProj = ($sizeProjAuth + $sizeProjCoed)) ?></span>
						</div>
						<div class="div-case <?php if($empty) echo "none" ?>"> <?php
							if(!$empty) { ?>
								<label>Owned:</label>
								<ol class="container"> <?php
									if(empty($sizeProjAuth)) {
										echo 'None';
									}
									else {
										while($obj = $resProjAuth->fetch_object()) { ?>
											<li class="proj">
												<a href="main.php?proj=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Title ?></a><br/>
												<span class="dim">Abstract: <?php echo Tool::truncate($obj->Abstr, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol>
								
								<label>Co-edited:</label>
								<ol class="container"> <?php
									if(empty($sizeProjCoed)) {
										echo 'None';
									}
									else {
										while($obj = $resProjCoed->fetch_object()) { ?>
											<li class="proj">
												<a href="main.php?proj=view&id=<?php echo $obj->ID ?>"><?php echo $obj->Title ?></a><br/>
												<span class="dim">Abstract: <?php echo Tool::truncate($obj->Abstr, 250) ?></span>
											</li> <?php
										}
									} ?>
								</ol> <?php
							} ?>
						</div> <?php
					} ?>
				</div> <?php
				
				# Side header # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ?>
				
				<div class="div-head">
					<img class="img file default ghost" src="<?php echo empty($edtr->ImgPath) ? (empty($imgPath) ? "template/acct.jpg" : $imgPath) : $edtr->ImgPath ?>" alt="<?php echo $edtr->Lname ?>"/>
					
					<div class="controls">
						<button class="stat dial peel on next" type="button"><b>Contents</b></button>
						<ul class="metas none"> <?php
							if(!empty($edtr->Author)) { ?>
								<li class="meta">Articles <span><?php echo $sizeArticle ?></span></li> <?php
							}
							
							if(!empty($edtr->Editor)) { ?>
								<li class="meta">Microorganisms <span><?php echo $sizeMicro ?></span></li>
								<li class="meta">Images <span><?php echo $sizeImg ?></span></li>
								<li class="meta">Documents <span><?php echo $sizeDoc ?></span></li>
								<li class="meta">Locations <span><?php echo $sizeLoc ?></span></li>
								<li class="meta">Resources <span><?php echo $sizeRsrc ?></span></li>
								<li class="meta">Projects <span><?php echo $sizeProj ?></span></li> <?php
							} ?>
						</ul> <?php
						
						$sett = new Setting('edtr', null, $this->mysqli, $this->user);
						$verified = $sett->verify($edtr->ID) and in_array($this->user['type'], array('PRIME','ADMIN'));
						
						if($verified) { ?>
							<button class="edit edtr dial" type="reset" data-id="<?php echo $edtr->ID ?>" data-email="<?php echo $edtr->Email ?>" data-auth="<?php echo $edtr->Author ?>" data-edtr="<?php echo $edtr->Editor ?>"
								data-lname="<?php echo $edtr->Lname ?>" data-fname="<?php echo $edtr->Fname ?>" data-mname="<?php echo $edtr->Mname ?>"><span>Edit</span></button> <?php
						} ?>
					</div>
				</div>
				
				<div class="div-mind">
					<input name="id" type="hidden" class="id abled" value="<?php echo $edtr->ID ?>"/>
					<input name="upd" type="checkbox" class="abled" value="<?php echo $edtr->ID ?>"/>
					<input name="del" type="checkbox" class="del abled" value="<?php echo $edtr->ID ?>"/>
					<input name="recov" type="checkbox" class="recov abled" value="<?php echo $edtr->ID ?>"/>
				</div>
			</div> <?php
			
			# Dial # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ?>
			
			<div class="div-dial"> <?php
				if($edtr->Remove=='0') { ?>
					<button class="delete tick read left" type="button" form="form-edtr<?php echo $edtr->ID ?>" data-area="edtr">Delete</button> <?php
				}
				else { ?>
					<button class="recover tick read left" type="button" form="form-edtr<?php echo $edtr->ID ?>" data-area="edtr">Recover</button> <?php
				} ?>
				<button type="button" form="form-edtr<?php echo $edtr->ID ?>" class="cancel edit read">Cancel</button>
				<button type="submit" class="read">Update</button> <?php
				
				if(!$cont) { ?>
					<button type="button" class="close-content reset">Close</button> <?php
				} ?>
			</div>
			</form>
		</div> <?php
	}
}
?>